package com.cocola.controller;

import com.cocola.pojo.Declaration;
import com.cocola.pojo.Education;
import com.cocola.service.DeclarationService;
import com.cocola.service.EducationSercice;
import com.cocola.service.UnitService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

import static com.cocola.controller.UserController.USERNAME;

@Controller
@CrossOrigin
public class DeclarationController {

    @Autowired
    private DeclarationService declarationService;

    @Autowired
    private UnitService unitService;

    @Autowired
    private EducationSercice educationSercice;

    /**
     *返回首页
     */
    @RequestMapping("/main")
    public String toMain(Model model) {
        if (USERNAME != null) {
            return "main";

        } else {
            model.addAttribute("error", "请登陆");
            return "login_person";
        }

    }


    @RequestMapping("/toApply")
    public String toApply(HttpSession session, Model model) {
        if (USERNAME != null) {
            return "apply";
        } else {
            model.addAttribute("error", "请登陆");
            return "login_person";
        }

    }

    @RequestMapping("/toDeclaration")
    public String toDeclaration(String unitName, HttpServletRequest request, HttpSession session) {
        try {

            System.out.println("unitName:" + unitName);

            if (!unitService.findOneUnit2(unitName).isEmpty()) {

                session.setAttribute("unitSendSession", unitName);
                session.setAttribute("userName", USERNAME);
                System.out.println("userName:" + USERNAME);
                return "apply2";

            } else {
                request.setAttribute("error", "单位不存在，请输入正确的单位名称");
                return "apply";
            }

        } catch (Exception e) {
            e.printStackTrace();
        }

        return "apply";
    }


    @RequestMapping("/doDeclaration")
    public String add(String unitName, String userType, String unitSend, String job, String major, String userName, String nation,
                      String idCard, String political, String birthTime, String foreignLanguage, String sex,
                      String special, String nativePlace, String birthPlace, String award, String phone,
                      String clubJob, String fileLocation, String photo, HttpSession session, Model model) {


        try {

            System.out.println("可以申请吗：" + declarationService.findForAdd(unitSend, major, USERNAME));

            if (declarationService.findForAdd(unitSend, major, USERNAME)) {

                declarationService.add(unitName, userType, unitSend, job, major, USERNAME, nation,
                        idCard, political, birthTime, foreignLanguage, sex, special, nativePlace,
                        birthPlace, award, phone, clubJob, fileLocation, photo);

                session.setAttribute("userName", USERNAME);
                System.out.println("userName:" + session.getAttribute("userName"));

                return "apply3";

            } else {
                model.addAttribute("error", "你已经申请过该单位的该专业了，请勿重复申请");
                return "apply2";
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return "apply2";
    }

    @RequestMapping("/doDeclaration2")
    public String add2(String userName, String nation, String idCard, String political, String birthTime,
                       String foreignLanguage, String sex, String special, String nativePlace,
                       String birthPlace, String award, String phone, String clubJob, String fileLocation, String photo) {

        try {

            declarationService.update(userName, nation, idCard, political, birthTime, foreignLanguage,
                    sex, special, nativePlace, birthPlace, award, phone, clubJob, fileLocation, photo);

            System.out.println("update done====================>");
            return "redirect:/toEducation";

        } catch (Exception e) {
            e.printStackTrace();
        }

        return "apply3";

    }

    @RequestMapping("/showDeclaration")
    public String show(Model model) {
        if (USERNAME != null) {
            return "apply4";

        } else {
            model.addAttribute("error", "请登陆");
            return "login_person";
        }
    }

    @RequestMapping("/findDeclaration")
    public String findDeclaration(Model model) {
        try {
            List<Declaration> declarationList = declarationService.findDeclaration();
            model.addAttribute("declarationList", declarationList);

            return "apply4";

        } catch (Exception e) {
            e.printStackTrace();
        }
        return "main";
    }

    @RequestMapping("/detail")
    public String detail(Model model) {
        try {
            if (USERNAME != null) {
                List<Declaration> declarationList = declarationService.findDeclaration();
                List<Education> educationList = educationSercice.findEducation();

                model.addAttribute("declarationList", declarationList);
                model.addAttribute("educationList", educationList);

                return "apply8";

            } else {
                model.addAttribute("error", "请登陆");
                return "login_person";
            }

        } catch (Exception e) {
            e.printStackTrace();
        }

        return "main";

    }


}
